package com.example.discover;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.common.network.IStartGameDetailActivity;
import com.example.discover.databinding.DiscoverBannerBinding;
import com.example.discover.databinding.DiscoverItemBinding;
import com.example.discover.viewmodel.DiscoverViewModel;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class DiscoverAdapter extends RecyclerView.Adapter<DiscoverAdapter.DiscoverHolder> {

    private static final String TAG = "DiscoverAdapter";

    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    DiscoverBannerBinding bannerBinding;
    private DiscoverViewModel viewModel;
    private LocalImageLoader localImageLoader;
    private ArrayList<String> imagePath;
    private ArrayList<Integer> idList;
    private View mHeaderView;

    public IStartGameDetailActivity iStartGameDetailActivity;//点击跳转接口

    //传递ViewModel
    public DiscoverAdapter(DiscoverViewModel discoverViewModel) {
        viewModel = discoverViewModel;
    }

    //根据pos返回不同的ItemViewType
    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }

    @NonNull
    @NotNull
    @Override
    public DiscoverHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {

        bannerBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.discover_banner, parent, false);
        initBannerData();   //初始化banner
        mHeaderView = bannerBinding.banner;
        DiscoverItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.discover_item, parent, false);
        DiscoverHolder holder = new DiscoverHolder(binding);
        DiscoverHolder bannerHolder = new DiscoverHolder(bannerBinding);
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return bannerHolder;
        } else {
            return holder;
        }
    }

    private void initBannerData() {
        idList = new ArrayList<>();
        imagePath = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            imagePath.add(viewModel.getGames().get(i).getBanner().getOriginalUrl());
            idList.add(viewModel.getGames().get(i).getGame().getId());
        }
        localImageLoader = new LocalImageLoader();
        Banner banner = bannerBinding.banner;

        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                iStartGameDetailActivity.openById(idList.get(position));
                Log.i("Banner", "OnBannerClick: " + position);
            }
        });
        //样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //加载器
        banner.setImageLoader(localImageLoader);
        //动画效果
        banner.setBannerAnimation(Transformer.Default);
        //间隔时间
        banner.setDelayTime(4500);
        //是否为自动轮播
        banner.isAutoPlay(true);
        //图片小点显示所在位置
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //图片加载地址
        banner.setImages(imagePath);
        //启动轮播图。
        banner.start();
    }


    @Override
    public void onBindViewHolder(@NonNull @NotNull DiscoverAdapter.DiscoverHolder holder, int position) {
        if (getItemViewType(position) == TYPE_HEADER) return;
        //分类
        holder.binding.setCategory(viewModel.getCategories().getValue().get(position - 1));
        //分类下的游戏
        holder.binding.setGame(viewModel.getDd().get(viewModel.getCategories().getValue().get(position - 1).getId()));
        //绑定游戏的点击事件
        holder.binding.discoverScrollViewItem0.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(0).getGame().getId()));
        holder.binding.discoverScrollViewItem1.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(1).getGame().getId()));
        holder.binding.discoverScrollViewItem2.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(2).getGame().getId()));
        holder.binding.discoverScrollViewItem3.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(3).getGame().getId()));
        holder.binding.discoverScrollViewItem4.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(4).getGame().getId()));
        holder.binding.discoverScrollViewItem5.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(5).getGame().getId()));
        holder.binding.discoverScrollViewItem6.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(6).getGame().getId()));
        holder.binding.discoverScrollViewItem7.setOnClickListener(v -> iStartGameDetailActivity.openById(holder.binding.getGame().get(7).getGame().getId()));
    }


    @Override
    public int getItemCount() {
        if (viewModel.getCategories().getValue() != null) {
            return mHeaderView == null ? viewModel.getCategories().getValue().size() : viewModel.getCategories().getValue().size() + 1;
        } else {
            return 0;
        }
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    /***************************************************************************************/
    class DiscoverHolder extends RecyclerView.ViewHolder {
        private DiscoverItemBinding binding;
        private DiscoverBannerBinding bannerBinding;

        public DiscoverHolder(DiscoverItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public DiscoverHolder(DiscoverBannerBinding binding) {
            super(binding.getRoot());
            if (binding.getRoot() == mHeaderView)
                return;
            this.bannerBinding = binding;
        }
    }

    /**
     * 图片加载类
     */
    private class LocalImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context.getApplicationContext())
                    .load(path)
                    .into(imageView);
        }
    }
}
